package com.bajiao.common.web;

import com.bajiao.common.web.entity.PageDomain;
import com.bajiao.common.web.entity.TableDataInfo;
import com.bajiao.common.web.entity.TableSupport;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.bajiao.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * web通用工具
 *
 * @author FanJq
 */
public class BaseController
{
    private static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    /**
     * 设置请求分页数据
     */
    protected void startPage()
    {
        PageDomain domain = TableSupport.getPageDomain();
        Integer pageSize = domain.getPageSize();
        Integer pageNum = domain.getPageNum();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            PageHelper.startPage(pageNum,pageSize);
        }
    }

    /**
     * 响应请求分页数据
     */
//    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(List<?> list)
    {
        return new TableDataInfo(list,(int) new PageInfo(list).getTotal());
    }
}
